// pages/confirm_order/confirm_order.js
let feedbackApi= require('../common/showToast.js');
const getToken= require('../../utils/get_token.js');

let memberId, openId, poodId = '', acType = '', pageSize = 1;
let cookie, token;
const ckToken = async (that) => {//token确认
  let nTime = Math.floor(new Date().getTime() / 1000);
  let oldTime = wx.getStorageSync('ntime') || 0;
  if (nTime < oldTime)//现在的时间小于到期时间
  {
    thou(that);
  } else {
    let tokens = await getToken.getToken();

    thou(that);
  }
}
const thou = (that) => {//token没有失效时执行的代码
  if (openId == '') {
    getToken.getOpenId(function (res) {
      console.log(res)
      wx.setStorageSync('openId', res.data.openId);
      openId = res.data.openId;
      detail(that);
    });
  } else {
    detail(that);
  }
}
const addcollect = (that) => {//添加取消收藏
  let params = ['shop/pingTuan/addHuoPinCollecRecord', {"openId": openId,"memberId":memberId,"goodsPinTuanId":poodId,"seeType":2,"isStatus":acType}]
  getToken.getData(function (res) {
    console.log(res.data)
    pageSize = 1;
    that.data.list = [];
    detail(that);
  }, ...params)

};
const detail = (that) => {
  that.setData({
    hidden: false
  })
  let params = ['shop/pingTuan/huoPinCollectionList', {"openId":openId,"memberId":memberId,"pageSize":pageSize,"seeType":2}]
  getToken.getData(function (res) {
    let date = new Date();
    date = date.getTime();
    let lists = res.data.list;
    if (lists.length > 0) {
      poodId = lists[0].id;
      for (let i = 0; i < lists.length; i++) {
        if (lists.endTime <= date) {
          lists[i].nshow = true;
        }
        lists[i].image = JSON.parse(lists[i].image);
        lists[i].img = 'https://www.globalhexi.com/' + lists[i].image[0].path + '/small/' + lists[i].image[0].id + '.jpg';
        lists[i].intPinPrice = (lists[i].intPinPrice / 100).toFixed(2);
        lists[i].intPrice = (lists[i].intPrice / 100).toFixed(2);
        that.data.list.push(lists[i]);
       }
     }
    if (that.data.list.length > 0) {
      that.data.type = 1;
    } else {
      that.data.type = 2;
    }
    that.setData({
      list: that.data.list,
      type: that.data.type,
      hidden: true
    })
  }, ...params)
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    boolT: true,
    disabled: false,
    type: 0,
    list: [],
    s: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    pageSize = 1;
    openId = wx.getStorageSync('openId') || '';
    memberId = wx.getStorageSync('memberId')||'';
    cookie = wx.getStorageSync('cookie') || '';
    token = wx.getStorageSync('token') || '';
    ckToken(that);
  },
  clear:function(e){//清空全部收藏
    let that=this;
    acType=999;
    addcollect(that);
  },
  clearOne:function(e){//清空单个
    let that = this;
    acType=0;
    poodId = e.target.dataset.id;
    addcollect(that);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },
  enter: function (e) {
    let that = this;
    if (that.data.boolT) {
      that.data.boolT = false;
      wx.navigateTo({
        url: '../paired_detail/paired_detail?scene=' + e.currentTarget.dataset.id,
        success: function () {
          setTimeout(function () {
            that.data.boolT = true;
          }, 500)
        }
      })
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    pageSize++;
    detail(that);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})